/* eslint-disable camelcase */
/* eslint-disable global-require */
/* eslint-disable func-names */
// eslint-disable-next-line import/no-commonjs
const path = require('path');

const { npm_package_version } = process.env;

const config = {
  framework: 'react',
  env: {
    VERSION: `"${process.env.VERSION}"`,
  },
  mini: {
    enableExtract: true,
    miniCssExtractPluginOption: {
      // 忽略css文件引入顺序
      ignoreOrder: true,
    },
    postcss: {
      pxtransform: {
        enable: true,
        config: {},
      },
      url: {
        enable: true,
        config: {
          limit: 10240, // 设定转换尺寸上限
        },
      },
      cssModules: {
        enable: true, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]',
        },
      },
    },
    webpackChain(chain) {
      chain.merge({
        module: {
          rule: {
            lessResourcesLoader: {
              test: /\.less|scss$/,
              use: [
                {
                  loader: 'style-resources-loader',
                  options: {
                    patterns: [path.resolve(__dirname, '..', 'src/styles/theme.<%= css %>')],
                  },
                },
              ],
            },
          },
        },
      });
    },
  },
  projectName: '<%= projectName %>',
  date: '2020-4-13',
  designWidth: 750,
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2,
  },
  sourceRoot: 'src',
  outputRoot: 'dist',
  alias: {
    '@/components': path.resolve(__dirname, '..', 'src/components'),
    '@/utils': path.resolve(__dirname, '..', 'src/utils'),
    '@/config': path.resolve(__dirname, '..', 'src/config'),
  },
  defineConstants: {
    SENTRY_CONFIG: JSON.stringify({
      dsn: 'https://9cea230d91cf48d59b8e7061ad17ccb6@sentry-test.biaoguoworks.com/3',
      environment: 'dev',
      release: npm_package_version,
    }),
  },
  h5: {
    publicPath: '/',
    staticDirectory: 'static',
    postcss: {
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]',
        },
      },
    },
  },
};

module.exports = function (merge) {
  switch (process.env.NODE_ENV) {
    case 'development':
      return merge({}, config, require('./dev'));
    case 'qa':
      return merge({}, config, require('./qa'));
    case 'stage':
      return merge({}, config, require('./stage'));
    case 'stage2':
      return merge({}, config, require('./stage2'));
    case 'stage3':
      return merge({}, config, require('./stage3'));
    case 'prod':
      return merge({}, config, require('./prod'));

    default:
      return merge({}, config, require('./dev'));
  }
};
